{% extends "base.html" %}
{% block title %}检查列表{% endblock %}
{% block head %}
    <meta HTTP-EQUIV="pragma" CONTENT="no-cache">
    <meta HTTP-EQUIV="Cache-Control" CONTENT="no-store, must-revalidate">
    <meta HTTP-EQUIV="expires" CONTENT="Wed, 26 Feb 1997 08:21:57 GMT">
    <meta HTTP-EQUIV="expires" CONTENT="0">
    {{ super() }}
    <style>
        .weui-cells__title {
            padding-left: 0;
        }

        .sheetList {
            display: none;
            background: #efeff4;
            border-left: 5px solid red;
        }

    </style>
{% endblock %}
{% block body %}
    {#    {% if not showTitle %}#}
    {#        {% import "component/header.html" as header %}#}
    {#        {{ header.header(true,"检查列表","") }}#}
    {#    {% endif %}#}
    <div class="contianer">
        <div class="mine-page" style="overflow-y: hidden">
            <div class="weui-group-dropdown">
                <div class="weui-flex btn-group" id="clinic_type_btnGroup">
                    <a href="javascript:void(0)" id="type-btn" data-value="0" class="group-btn">
                        <div>超声</div>

                        <div class="drop-down">
                            <i class="iconfont">&#xe63e;</i>
                        </div>
                    </a>
                    <a href="javascript:void(0)" id="time-btn" data-value="1" class="group-btn">
                        <div>七天内</div>

                        <div class="drop-down">
                            <i class="iconfont">&#xe63e;</i>
                        </div>
                    </a>
                    <a href="javascript:void(0)" id="state-btn" data-value="2" class="group-btn">
                        <div>未报到</div>

                        <div class="drop-down">
                            <i class="iconfont">&#xe63e;</i>
                        </div>
                    </a>
                </div>
                <div id="type-drop-down" class="weui-flex weui-dropdown">
                    {% for item in typeList %}
                        <div class="weui-flex__item weui-cell {{ 'selected' if item.id=="3" }}"
                             data-value="{{ item.id }}">{{ item.type }}</div>
                    {% endfor %}
                </div>
                <div id="time-drop-down" class="weui-flex weui-dropdown">
                    <div class="weui-flex__item weui-cell" data-value="0">今天</div>
                    <div class="weui-flex__item weui-cell selected" data-value="-7">七天内</div>
                    <div class="weui-flex__item weui-cell" data-value="-30">一个月</div>
                    <div class="weui-flex__item weui-cell" data-value="-182">近半年</div>
                    <div class="weui-flex__item weui-cell" data-value="-365">近一年</div>
                </div>
                <div id="state-drop-down" class="weui-flex weui-dropdown">
                    <div class="weui-flex__item weui-cell selected" data-value="1">未报到</div>
                    <div class="weui-flex__item weui-cell" data-value="2">已报到</div>
                    <div class="weui-flex__item weui-cell" data-value="3">已检查</div>
                    <div class="weui-flex__item weui-cell" data-value="4">报告发布</div>
                    <div class="weui-flex__item weui-cell" data-value="5">退检</div>
                </div>
            </div>
            <div class="ccdList-page" style="overflow-x: hidden;top: 4em;">
                <div class="weui-pull-to-refresh__layer" style="padding:7px;">
                    <div class='weui-pull-to-refresh__arrow'></div>
                    <div class='weui-pull-to-refresh__preloader'></div>
                    <div class="down">下拉刷新</div>
                    <div class="up">释放刷新</div>
                    <div class="refresh">正在刷新</div>
                </div>

                <div id="ccd-list" style="top:3em;">
                </div>
                <div class="weui-infinite-scroll" style="display: none;">
                    <div class="infinite-preloader"></div>
                    <div class="weui-loadmore" style="margin: 0 auto">
                        <i class="weui-loading"></i>
                        <span class="weui-loadmore__tips">正在加载</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
{% endblock %}
{% block script %}
    {{ super() }}
    <script type="text/javascript">

        var cacheDataList = null;
        var select_type = 3;
        var select_time = -7;
        var select_state = 1;
        var batchRes = [];//批量预约的预约号
        var user_id = common.getQueryString("user_id");
        //点击下拉列表，显示不同的筛选数据
        $(".weui-dropdown>.weui-flex__item").click(function () {
            event.stopPropagation();
            var typeValue = $(this).attr("data-value");
            var typeName = $(this).text().trim();
            var groupName = $(this).parent().attr("id");
            $(this).parent().find(".selected").removeClass("selected");
            $(this).addClass("selected");
            if (groupName == "type-drop-down") {
                select_type = typeValue;
                $("#clinic_type_btnGroup>#type-btn>div:first-child").text(typeName);
                {#sessionStorage.setItem("list_type", select_type);#}
            } else if (groupName == "time-drop-down") {
                select_time = parseInt(typeValue);
                $("#clinic_type_btnGroup>#time-btn>div:first-child").text(typeName);
                {#sessionStorage.setItem("list_time", select_time);#}
            } else if (groupName == "state-drop-down") {
                select_state = parseInt(typeValue);
                $("#clinic_type_btnGroup>#state-btn>div:first-child").text(typeName);
            }
            $(".group-btn").removeClass("selected");
            $(".weui-dropdown").slideUp("fast");
            $("#ccd-list").empty();
            getCCDList();
        });
        //根据点击的group-btn显示不同的下拉列表
        $(".group-btn").click(function () {
            event.preventDefault();
            event.stopPropagation();
            $(this).siblings().removeClass("selected");
            if ($(this).hasClass("selected")) {
                $(this).removeClass("selected");
                $(".weui-dropdown").slideUp("fast");
            } else {
                $(this).addClass("selected");
                $(".weui-dropdown").hide();
                if ($(this).attr("data-value") == "0") {
                    $("#type-drop-down.weui-dropdown").slideDown("fast");
                } else if ($(this).attr("data-value") == "1") {
                    $("#time-drop-down.weui-dropdown").slideDown("fast");
                } else if ($(this).attr("data-value") == "2") {
                    $("#state-drop-down.weui-dropdown").slideDown("fast");
                }
            }
        });

        $(function () {
            if (sessionStorage.getItem("list_type") != null) {
                select_type = parseInt(sessionStorage.getItem("list_type"));
                $("#type-drop-down").find(".weui-flex__item.selected").removeClass("selected");
                $("#type-drop-down>.weui-flex__item[data-value='" + select_type + "']").addClass("selected");
                $("#clinic_type_btnGroup>#type-btn>div:first-child").text($("#type-drop-down>.weui-flex__item[data-value='" + select_type + "']").text());
                {#                sessionStorage.removeItem("list_type");#}

            }
            if (sessionStorage.getItem("list_time") != null) {
                select_time = parseInt(sessionStorage.getItem("list_time"));
                $("#time-drop-down").find(".weui-flex__item.selected").removeClass("selected");
                $("#time-drop-down>.weui-flex__item[data-value='" + select_time + "']").addClass("selected");
                $("#clinic_type_btnGroup>#time-btn>div:first-child").text($("#time-drop-down>.weui-flex__item[data-value='" + select_time + "']").text());
                {#                sessionStorage.removeItem("list_time");#}
            }
            if (sessionStorage.getItem("list_state") != null) {
                select_state = parseInt(sessionStorage.getItem("list_state"));
                $("#state-drop-down").find(".weui-flex__item.selected").removeClass("selected");
                $("#state-drop-down>.weui-flex__item[data-value='" + select_state + "']").addClass("selected");
                $("#clinic_type_btnGroup>#state-btn>div:first-child").text($("#state-drop-down>.weui-flex__item[data-value='" + select_state + "']").text());
                {#                sessionStorage.removeItem("list_state");#}
            }
            getCCDList();
            $(".ccdList-page").pullToRefresh({
                onRefresh: function () { /* 当下拉刷新触发的时候执行的回调 */
                    $("#ccd-list").empty();
                    getCCDList("");
                    $(".ccdList-page").pullToRefreshDone();
                },
                onPull: function (percent) { /* 用户下拉过程中会触发，接收一个百分比表示用户下拉的比例 */
                },
                distance: 50 /* 下拉刷新的触发距离， 注意，如果你重新定义了这个值，那么你需要重载一部分CSS才可以，请参考下面的自定义样式部分 */
            });

        })

        function getCCDList() {
            $("#ccd-list").empty();
            var types = [];
            $.showLoading();

            var params = {
                "patient_id": common.getQueryString("user_id"),
                "select_type": select_type,
                "select_state": select_state
            };
            var start_time = common.getNextDay(select_time);
            var end_time = common.getNow();
            $.post(navRoot + "/api/inpatient/list", params, function (data) {
                if (data["code"] == 200) {
                    var list = [];
                    try {
                        if (data["data"]["List"]) {

                            var dataObj = data["data"]["List"]["RequisitionFindResult"]["MedicalRequestInfo"]["RequestInfo"];
                            var patientInfo = data["data"]["List"]["RequisitionFindResult"]["PatientInfo"];
                            if (!dataObj.hasOwnProperty("length")) {
                                list[0] = dataObj;
                            } else {
                                list = dataObj;
                            }
                            cacheDataList = list;
                            for (var i in list) {
                                var requestInfo = list[i];
                                if (common.dateFormart(requestInfo["ReqSheetDate"], "yyyy-MM-dd") > start_time) {
                                    var exam_ScheduleID = requestInfo['ReservationInfo']["ScheduleID"];
                                    if (!exam_ScheduleID || (exam_ScheduleID && types.indexOf(exam_ScheduleID.replace("@", "-")) == -1)) {
                                        if (exam_ScheduleID) {
                                            exam_ScheduleID = exam_ScheduleID.replace("@", "-");
                                            types.push(exam_ScheduleID);
                                        }

                                        var typeItem = $('<div class="weui-well weui-well_access type' + exam_ScheduleID + '">' +
                                            '<div class="weui-well__top weui-flex">' +
                                            '    <div class="weui-flex__item inOrOutTime">' + requestInfo["ExamList"]["Exam"]["ExamProject"] + '</div>' +
                                            '    <div class="exam_state font12 font-bold text-common">' + '</div>' +
                                            '</div>' +
                                            '<div class="weui-well__content">' +
                                            '   <div class="weui-flex">' +
                                            '       <div class="weui-flex__item font12 text-disabled">临床诊断：' + requestInfo["LinChuangZhenDuan"] + '</div>' +
                                            '   </div>' +
                                            '<div class="weui-cells__title" style="display:none">同一个预约的其他申请单</div>' +
                                            '   <div class="sheetList weui-cells">' +
                                            '   </div>' +
                                            '</div>' +
                                            '<div class="weui-well__footer weui-flex">' +
                                            '       <div class="weui-flex__item font12 text-disabled">预约时间：' + common.dateFormart(requestInfo["ReservationInfo"]["resDate"], "yyyy-MM-dd") + " " + common.dateFormart(requestInfo["ReservationInfo"]["resTime"]) + '</div>' +
                                            '       <button class="baodaoBtn weui-btn weui-btn_mini">报到</button>' +
                                            '</div>' +
                                            '</div>'
                                        );
                                        var resDate = common.dateFormart(requestInfo["ReservationInfo"]["resDate"], 'yyyy-MM-dd');
                                        var resTime = requestInfo["ReservationInfo"]["resTime"];
                                        if (resDate) {

                                            if (resTime && resTime.split("-").length > 1 && common.isOutTime(resDate, resTime.split("-")[1])) {
                                                typeItem.find(".baodaoBtn").addClass("weui-btn_disabled");
                                                typeItem.find(".baodaoBtn").click(function (event) {
                                                    event.preventDefault();
                                                    event.stopPropagation();
                                                    $.alert("报到时间已过，请重新预约");
                                                });
                                            } else if (resTime && resTime.split("-").length > 1 && common.isDaoTime(resDate, resTime.split("-")[0])) {
                                                typeItem.find(".baodaoBtn").addClass("weui-btn_disabled");
                                                typeItem.find(".baodaoBtn").click(function (event) {
                                                    event.preventDefault();
                                                    event.stopPropagation();
                                                    $.alert("报到时间未到，请预约时间进行操作");
                                                });
                                            } else {
                                                var ScheduleID = requestInfo["ReservationInfo"]["ScheduleID"];
                                                if (ScheduleID && batchRes.indexOf(ScheduleID) == -1) {
                                                    batchRes.push({
                                                        "sheetID": requestInfo["SheetID"],
                                                        "scheduleID": ScheduleID,
                                                        "type": select_type
                                                    });
                                                }
                                                typeItem.find(".baodaoBtn").data("data", requestInfo);
                                                typeItem.find(".baodaoBtn").click(function (event) {
                                                    event.preventDefault();
                                                    event.stopPropagation();
                                                    var requestInfo = $(this).data("data");
                                                    var params = {
                                                        "sheetID": requestInfo["SheetID"],
                                                        "scheduleID": requestInfo["ReservationInfo"]["ScheduleID"],
                                                        "type": select_type
                                                    };
                                                    $.post(navRoot + "/api/inpatient/baodao", params, function (data) {
                                                        if (data["code"] == 200) {
                                                            var result = data["data"];
                                                            sessionStorage.setItem("list_scrollTop", $(".ccdList-page").scrollTop());
                                                            sessionStorage.setItem("list_type", select_type);
                                                            sessionStorage.setItem("list_time", select_time);
                                                            location.href = navRoot + "/info?scheduleID=" + (params["scheduleID"] || "") + "&sheetID=" + params["sheetID"] + "&type=" + params["type"] + "&queueName=" + result["QueueName"] + "&queueNumber=" + result["QueueNumber"];
                                                        } else {
                                                            $.alert(data["errmsg"]);
                                                        }
                                                    }, "json")
                                                })
                                            }
                                        } else {
                                            typeItem.find(".baodaoBtn").addClass("weui-btn_disabled");
                                            typeItem.find(".baodaoBtn").click(function (event) {
                                                event.preventDefault();
                                                event.stopPropagation();
                                                $.alert("无预约时间，无法报到");
                                            });
                                        }

                                        var examState = requestInfo["ExamList"]["Exam"]["ExamState"];
                                        if (examState == "1") {
                                            typeItem.find(".exam_state").text("未报到");
                                        } else if (examState == "2") {
                                            typeItem.find(".exam_state").text("已报到");
                                        } else if (examState == "3") {
                                            typeItem.find(".exam_state").text("已检查");
                                        } else if (examState == "4") {
                                            typeItem.find(".exam_state").text("报告发布");
                                        } else if (examState == "5") {
                                            typeItem.find(".exam_state").text("退检");
                                        }
                                        typeItem.find(".weui-cells__title").click(function () {
                                            event.preventDefault();
                                            event.stopPropagation();
                                            $(this).parent().find(".sheetList").slideToggle('fast');
                                        });
                                        typeItem.data("info", requestInfo);
                                        typeItem.click(function () {
                                            var id = $(this).data("info")["SheetID"];
                                            var scheduleID = $(this).data("info")["ReservationInfo"]["ScheduleID"];
                                            var type = $(this).data("info")["RequisitionType"];
                                            sessionStorage.setItem("list_scrollTop", $(".ccdList-page").scrollTop());
                                            sessionStorage.setItem("list_type", select_type);
                                            sessionStorage.setItem("list_time", select_time);
                                            sessionStorage.setItem("list_state", select_state);
                                            location.href = navRoot + "/info?sheetID=" + id + "&scheduleID=" + (scheduleID || "") + "&type=" + type;
                                        });
                                        $("#ccd-list").append(typeItem);
                                    } else {
                                        var yyTypeItem = $("#ccd-list").find(".weui-well.type" + exam_ScheduleID.replace("@", "-"));
                                        var item = $(
                                            '<div class="weui-cell weui-cell_access" style="padding:5px;">' +
                                            '       <div class="weui-cell__bd">' +
                                            '           <div class="font14">' + requestInfo["ExamList"]["Exam"]["ExamProject"] + '</div>' +
                                            '           <div class="font12 text-disabled">预约时间：' + common.dateFormart(requestInfo["ReservationInfo"]["resDate"], "yyyy-MM-dd") + " " + common.dateFormart(requestInfo["ReservationInfo"]["resTime"]) + '</div>' +
                                            '       </div>' +
                                            '       <div class="weui-cell__ft"></div>' +
                                            '</div>');
                                        item.data("info", requestInfo);
                                        item.click(function () {
                                            event.preventDefault();
                                            event.stopPropagation();
                                            var id = $(this).data("info")["SheetID"];
                                            var type = $(this).data("info")["RequisitionType"];
                                            sessionStorage.setItem("list_scrollTop", $(".ccdList-page").scrollTop());
                                            sessionStorage.setItem("list_type", select_type);
                                            sessionStorage.setItem("list_time", select_time);
                                            sessionStorage.setItem("list_time", select_state);
                                            location.href = navRoot + "/info?sheetID=" + id + "&type=" + type;
                                        });
                                        yyTypeItem.find(".weui-cells__title").show();
                                        yyTypeItem.find(".weui-well__content>.sheetList").append(item);
                                    }
                                }
                            }
                            //如果存在两个或者以上满足条件的预约或申请单的话，则可以进行批量报到
                            if (batchRes.length > 1) {
                                var batItem = $('<div class="weui-loadmore" style="width:85%;"><div class="weui-btn weui-btn_primary">批量报到</div></div>');
                                batItem.click(function () {
                                    for (var r in batchRes) {
                                        var pars = batchRes[r];
                                        $.post(navRoot + "/api/inpatient/baodao", pars, function (data) {
                                            if (data["code"] == 200) {
                                                if (batchRes.lastIndexOf(pars) == 0) {
                                                    location.reload();
                                                }
                                            } else {
                                                $.alert(data["errmsg"]);
                                            }
                                        }, "json")
                                    }
                                })

                                $("#ccd-list").append(batItem);
                            }
                            if ($("#ccd-list").children().length == 0) {
                                $("#ccd-list").append('<div class="weui-loadmore weui-loadmore_line"><span class="weui-loadmore__tips">暂无数据</span></div>');
                            }
                        } else {
                            $("#ccd-list").append('<div class="weui-loadmore weui-loadmore_line"><span class="weui-loadmore__tips">暂无数据</span></div>');
                        }
                        if (sessionStorage.getItem("list_scrollTop")) {
                            $(".ccdList-page").scrollTop(sessionStorage.getItem("list_scrollTop"));
                            sessionStorage.removeItem("list_scrollTop");
                        }
                    } catch (err) {
                        $.alert("数据错误:" + err);
                    }
                } else if (data["code"] == 501) {
                    $("#ccd-list").append('<div class="weui-loadmore weui-loadmore_line"><span class="weui-loadmore__tips">暂无数据</span></div>');
                } else {
                    $.alert(data["errmsg"]);
                }
                $.hideLoading();
            }, "json")
        }
    </script>
{% endblock %}